var utils = require('../../../utils/util.js')
var c = require('../../../utils/config.js')
Page({
    data: {
        addrList: [],
    },
    onLoad: function (options) {
        this.getaddrList();
    },
    onShow:function(){
        this.getaddrList();
    },
    //下拉刷新
    onPullDownRefresh: function () {
      this.setData({
        addrList: [],
      });
      this.getaddrList();
    },
    getaddrList:function(){
        var that = this;
        var token = wx.getStorageSync("userinfo").wap_token;
        var data = {
            url: "/wsgoods/Useraddress/lists",
            token: token,
        };
        utils.request(data).then(function (res) {
            if (res.code == 0) {
                var data = res.data;
                that.setData({
                    addrList: data
                });
            } else {

            }
            wx.stopPullDownRefresh();
        });
    },
    delAddr:function(e){
        var id = e.currentTarget.dataset.id;
        var that = this;
        wx.showModal({
            title: '提示',
            content: '确认要删除地址吗?',
            success: function (res) {
                if (res.confirm) {
                    var token = wx.getStorageSync("userinfo").wap_token;
                    var data = {
                        url: "/wsgoods/Useraddress/del",
                        token: token,
                        id:id
                    };
                    utils.request(data).then(function (res) {
                        if (res.code == 0) {
                            wx.showToast({
                                title: "删除成功",
                                icon: "success"
                            })
                            setTimeout(function(){
                                that.getaddrList()
                            },2000);
                        } else {
                            wx.showToast({
                                title: res.msg,
                                icon:"none"
                            })
                        }
                    });
                } else if (res.cancel) {
                    
                }
            }
        })
    },
    //设置默认地址
    changeDefault:function(e){
        var id = e.currentTarget.dataset.id;
        var that = this;
        var token = wx.getStorageSync("userinfo").wap_token;
        var data = {
            url: "/wsgoods/Useraddress/setDefault",
            token: token,
            id: id
        };
        utils.request(data).then(function (res) {
            if (res.code == 0) {
                utils.sucMsg("修改成功");
                that.getaddrList()
            } else {
                utils.errMsg(res.msg);
            }
        });
    }
})